package com.xtianzhuang.www.controller.user.work;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.TfSlowSql;
import com.xtianzhuang.www.entity.TfUserDailyRecord;
import com.xtianzhuang.www.entity.TfWorkRecord;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.QueryDailyLifeRequest;
import com.xtianzhuang.www.entity.request.QueryDailyWorkRequest;
import com.xtianzhuang.www.entity.request.SaveSqlImproveRequest;
import com.xtianzhuang.www.entity.request.SaveWorkRequest;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.DailyRecord;
import com.xtianzhuang.www.entity.response.QueryDailyLifeResponse;
import com.xtianzhuang.www.entity.response.QueryDailyWorkResponse;
import com.xtianzhuang.www.entity.response.SaveSqlImproveResponse;
import com.xtianzhuang.www.entity.response.SaveWorkResponse;
import com.xtianzhuang.www.entity.response.UploadSQLXmlResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;

@Controller
@RequestMapping("/work")
public class WorkController extends BaseController {
	@RequestMapping(value = "/addWorkRecord")
	public ModelAndView addWorkRecord(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/work-record-add");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		return modelAndView;
	}

	@RequestMapping(value = "/daily-work")
	public ModelAndView dailyWork(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/daily-work");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		return modelAndView;
	}

	@RequestMapping(value = "/daily-life-statistics")
	public ModelAndView dailyWorkStatistics(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/daily-life-statistics");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		List<DailyRecord> list = workServiceBiz.getDailyRecordByMonth();
		List<String> dateList = new LinkedList<String>();
		List<String> isReadList = new LinkedList<String>();
		List<String> isExerciseList = new LinkedList<String>();
		List<String> isGetUpEarlyList = new LinkedList<String>();
		List<String> isSleepEarlyList = new LinkedList<String>();
		for (DailyRecord record : list) {
			dateList.add("'" + record.getDate() + "'");
			isReadList.add(record.getIsRead());
			isExerciseList.add(record.getIsExercise());
			isGetUpEarlyList.add(record.getIsGetUpEarly());
			isSleepEarlyList.add(record.getIsSleepEarly());
		}
		modelAndView.addObject("date", dateList);
		modelAndView.addObject("isReadList", isReadList);
		modelAndView.addObject("isExerciseList", isExerciseList);
		modelAndView.addObject("isGetUpEarlyList", isGetUpEarlyList);
		modelAndView.addObject("isSleepEarlyList", isSleepEarlyList);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/sqlImport")
	public ModelAndView sqlImport(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("work/sql-import");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/sqlImprove")
	public ModelAndView sqlImprove(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("work/sql-improve");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		return modelAndView;
	}


	@RequestMapping(value = "/daily-life")
	public ModelAndView dailyLife(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("user/daily-life");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		return modelAndView;
	}

	@RequestMapping("/ajaxSaveSqlImprove")
	@ResponseBody
	public SaveSqlImproveResponse ajaxSaveSqlImprove(SaveSqlImproveRequest request) {
		SaveSqlImproveResponse response = new SaveSqlImproveResponse();
		try {
			workServiceBiz.saveSqlImprove(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}


	@RequestMapping("/ajaxSaveWork")
	@ResponseBody
	public SaveWorkResponse saveWork(SaveWorkRequest request) {
		SaveWorkResponse response = new SaveWorkResponse();
		try {
			if (userServiceBiz.checkWorkRequest(request)) {
				userServiceBiz.saveWork(request);
				response.setCode(CodeConstants.SUCCESS);
				response.setMessage("保存成功");
			} else {
				response.setCode("9999");
				response.setMessage("当日日志已保存,不能重复添加");
			}
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/ajaxQueryDailyLife")
	@ResponseBody
	@Log(type = LogType.USER)
	public QueryDailyLifeResponse queryDailyLife(QueryDailyLifeRequest request) {
		QueryDailyLifeResponse response = new QueryDailyLifeResponse();
		try {
			long count = userServiceBiz.queryDailyLifeCount(request);
			List<TfUserDailyRecord> list = userServiceBiz.queryDailyLife(request);
			response.setData(list);
			response.setTotal(count);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@RequestMapping("/ajaxQueryDailyWork")
	@ResponseBody
	@Log(type = LogType.USER)
	public QueryDailyWorkResponse queryDailyWork(QueryDailyWorkRequest request) {
		QueryDailyWorkResponse response = new QueryDailyWorkResponse();
		try {
			long count = userServiceBiz.queryDailyWorkCount(request);
			List<TfWorkRecord> list = userServiceBiz.queryDailyWork(request);
			response.setData(list);
			response.setTotal(count);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setCode(CodeConstants.FAIL);
			response.setMessage(e.getMessage());
		}
		return response;
	}


	@RequestMapping(value = "/uploadSQLXml")
	@ResponseBody
	public UploadSQLXmlResponse uploadSQLXml(MultipartFile file, HttpServletRequest request) {
		UploadSQLXmlResponse response = new UploadSQLXmlResponse();
		// 读取IO流文件
		InputStream input = null;
		HSSFWorkbook wb = null;
		try {
			input = file.getInputStream();
			wb = new HSSFWorkbook(input);
			// 读取页
			for (int sheetNum = 0; sheetNum < wb.getNumberOfSheets(); sheetNum++) {
				HSSFSheet xssfSheet = wb.getSheetAt(sheetNum);
				if (xssfSheet == null) {
					continue;
				}
				// 读取行
				for (int rowNum = 1; rowNum < xssfSheet.getLastRowNum() + 1; rowNum++) {
					TfSlowSql tfSlowSql = new TfSlowSql();
					HSSFRow row = xssfSheet.getRow(rowNum);
					if (row != null) {
						for (int cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
							if (cellNum == 1) {
								tfSlowSql.setOldExecuteTime(row.getCell(cellNum).getStringCellValue());
							}
							if (cellNum == 6) {
								String str = row.getCell(cellNum).getStringCellValue();
								if (str.indexOf("// */") > 0) {
									str = str.substring(str.indexOf("// */") + 5);
								}
								tfSlowSql.setOldSql(str);
							}
						}
					}
					tfSlowSql.setCreateStaffId("zxt");
					tfSlowSql.setCreateTime(new Date());
					tfSlowSql.setState("00");
					workServiceBiz.insertTfSlowSql(tfSlowSql);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return response;
	}


}
